package com.leetcode.two_pointers.sliding_window;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @author Dennis Li
 * @date 2020/9/23 11:05
 */
public class MinWindow_76 {

    public String minWindow(String s, String t) {
        Map<Character, Integer> window = new HashMap<>(), need = new HashMap<>();
        // 用来记录满足字符的个数
        int valid = 0;
        for (char c : t.toCharArray()) {
            need.put(c, need.getOrDefault(c, 0) + 1);

        }
        int left = 0, right = 0, start = 0, end = Integer.MAX_VALUE;
        while (right < s.length()) {
            char rc = s.charAt(right);
            right++;
            if (need.containsKey(rc)) {
                window.put(rc, window.getOrDefault(rc, 0) + 1);
                if (window.get(rc).equals(need.get(rc)))
                    valid++;
            }
            while (valid == need.size()) {
                if (right - left < end - start) {
                    start = left;
                    end = right;
                }
                char lc = s.charAt(left);
                if (window.containsKey(lc)) {
                    int num = window.get(lc);
                    if (num == need.get(lc)) {
                        valid--;
                    }
                    window.put(lc, num - 1);
                }
                left++;
            }
        }
        return end == Integer.MAX_VALUE ? "" : s.substring(start, end);
    }

    static class Solution {
        public static void main(String[] args) {
            LinkedList list = new LinkedList();
            list.add("a");
            list.add(2, "b");
            String s = (String) list.get(1);
            System.out.println(s);
        }
        public String minWindow(String s, String t) {

            Map<Character, Integer> window = new HashMap<>(), need = new HashMap<>();
            for (int i = 0; i < t.length(); i++) {
                char c = t.charAt(i);
                need.put(c, need.getOrDefault(c, 0) + 1);
            }
            int right = 0, left = 0, start = 0, end = Integer.MAX_VALUE, valid = 0;
            while (right < s.length()) {
                char rc = s.charAt(right);
                right++;
                if (need.containsKey(rc)) {
                    int oldVal = window.getOrDefault(rc, 0);
                    window.put(rc, oldVal + 1);
                    if (need.get(rc) == oldVal + 1) {
                        valid++;
                    }
                }
                while (valid == need.size()) {
                    if (right - left < end - start) {
                        end = right;
                        start = left;
                    }
                    char lc = s.charAt(left);
                    if (window.containsKey(lc)) {
                        int oldVal = window.get(lc);
                        window.put(lc, oldVal - 1);
                        if (oldVal == need.get(lc))
                            valid--;
                    }
                    left++;
                }
            }
            return end == Integer.MAX_VALUE ? "" : s.substring(start, end);
        }
    }

}
